var ws = new WebSocket("ws://localhost:8765");

var timeConnect = 0;
var timeEnd = 0;

ws.onopen = function(event) {
	console.log('WebSocket connected');
};
ws.onmessage = function(event) {
	$('<li>&lt;&nbsp;' + event.data + '</li>').appendTo('#chat-list');
	console.log('< ', event.data);
};
ws.onerror = function(event) {
	console.warn('[ERROR] An error occured');
};
ws.onclose = function(event) {
	timeEnd = new Date().getTime();
	console.log('WebSocket closed. Connection time: ', timeEnd - timeConnect);
};

function sendMessage(msg) {
	timeConnect = new Date().getTime();
	ws.send(msg);
	$('<li>&gt;&nbsp;' + msg + '</li>').appendTo('#chat-list');
	$('#text').val('').attr('value', '');
};

$('#send').click(function(event) {
	var msg = $('#text').val();
	if ((!msg) || (msg == "")) return;
	sendMessage(msg);
});